package com.ebook.server.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ebook.server.service.BookService;
import com.ebook.server.service.BookmarkService;
import com.ebook.server.service.NoteService;
import com.ebook.server.service.PartnerService;
import com.ebook.server.service.UserService;

@Controller
@RequestMapping(value = "/book")
public class BookController {

	public static final String UPLOAD_TEMP_PATH = "./";
	@Resource
	private BookService bookService;

	@Resource
	private NoteService noteService;

	@Resource
	private UserService userService;

	@Resource
	private BookmarkService bookmarkService;
	


	/**
	 * TODO 测试本地断点续传下载的代码  --待删除
	*/
//	@RequestMapping(value = "/download")
//	public ModelAndView download_new(HttpServletRequest request, HttpServletResponse response){
//		ModelAndView modelView = new ModelAndView();
//		ModelMap modelMap = new ModelMap();
//		String filePath = "C:\\opt\\ebook\\data\\note\\1\\aa.zip";
//		UpAndDown.continueToDownload(filePath, request, response);
//		return modelView;
//	}
	
	
	@RequestMapping(value = "/getNewBooks")
	public ModelAndView getNewBooks(@RequestParam("uid") int uid, @RequestParam("bookidsJson") String bookidsJson,
			@RequestParam("CODE") String verificationCode) {
		return bookService.getNewBooks(uid, bookidsJson, verificationCode);

	}
		 	
	/**
	 * 支持断点续传下载用户图书的功能
	 * @param uid      用户id
	 * @param bookid   图书id
	 * @param request
	 * @param response
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/downloadEbook")
	public ModelAndView downloadEbook(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("CODE") String verificationCode,
			HttpServletRequest request, HttpServletResponse response) {
		return bookService.downloadEbook(uid, bookid,verificationCode, request, response);
	}
	/**
	 * 验证注册码功能
	 * @param uid      用户id
	 * @param  CODE  图书id
	 * @param request
	 * @param response
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/validateCode")
	public ModelAndView validateCode(@RequestParam("uid") int uid, 
			@RequestParam("CODE") String verificationCode,
			HttpServletRequest request, HttpServletResponse response) {
		return bookService.validate(uid, verificationCode, request, response);
	}
	
	/**
	 * 验证注册码功能
	 * @param uid      用户id
	 * @param  CODE  图书id
	 * @param request
	 * @param response
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/deleteCode")
	public ModelAndView  deleteCode(@RequestParam("uid") int uid, 
			@RequestParam("CODE") String verificationCode,
			HttpServletRequest request, HttpServletResponse response) {
		 return bookService.deleteCode(uid, verificationCode, request, response);
	}
	
	
	
	/**
	 * 下载加密的图书文件
	 * @param uid
	 * @param bookid
	 * @param deviceId 设备Id
	 * @param request
	 * @param response
	 * @return 
	*/
	@RequestMapping(value = "/encryptedEbook")
	public ModelAndView encryptedEbook(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("device_id") String deviceId,@RequestParam("CODE") String verificationCode) {
		return bookService.encrypteAndCompression(uid, bookid,deviceId,verificationCode);
	}
	
	
	
	/**
	 * 断点续传下载加密后的文件，以免重复加密
	 * @param uid
	 * @param bookid
	 * @param deviceId 设备Id
	 * @param request
	 * @param response
	 * @return 
	*/
	@RequestMapping(value = "/continueToDownload")
	public ModelAndView continueToDownload(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("device_id") String deviceId,@RequestParam("CODE") String verificationCode,
			HttpServletRequest request, HttpServletResponse response) {
		return bookService.continueToDownload(uid, bookid,deviceId,verificationCode, request, response);
	}
	

	/**
	 * 文件下载成功后的操作
	 * @param uid
	 * @param bookid
	 * @param deviceId 设备id
	 * @return 密钥信息
	*/
	@RequestMapping(value = "/downSuccessfulOperation")
	public ModelAndView downSuccessfulOperation(@RequestParam("uid") int uid,@RequestParam("bookid") int bookid,
			@RequestParam("device_id") String deviceId,@RequestParam("CODE") String verificationCode) {
		return bookService.downSuccessfulOperation(uid,bookid,deviceId,verificationCode);
	}

	
	
	
	/**
	 * 删除一条或多条笔记功能
	 * @param noteIdsJson 笔记ids
	 * @param uid         用户id
	 * @param request
	 * @return            ModelAndView
	 */
	@RequestMapping(value = "/delNote",method = RequestMethod.POST)
	public ModelAndView deleteNote(@RequestParam("noteIdsJson") String noteIdsJson, @RequestParam("uid") int uid,
			@RequestParam("bookid") int bookid,@RequestParam("device_id") String device_id,
			@RequestParam("CODE") String verificationCode) {
		return noteService.delNoteAndZip(noteIdsJson, uid,bookid,device_id,verificationCode);
	}
	
	@RequestMapping(value = "/determineSync")
	public ModelAndView determineSync(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("device_id") String device_id,@RequestParam("CODE") String verificationCode) {
		return noteService.determineSync(uid, bookid,device_id,verificationCode);
	}
	
	
	@RequestMapping(value = "/syncNote")
	public ModelAndView syncNote(@RequestParam(value = "noteFile", required = false) MultipartFile noteFile,
			@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,@RequestParam(value = "isSync", required = false) String isSync,
			@RequestParam("device_id") String device_id,@RequestParam("noteJson") String noteJson,
			@RequestParam("CODE") String verificationCode) {
		return noteService.syncNote(noteFile,uid, bookid,device_id,isSync, noteJson,verificationCode);
	}
	
	
	@RequestMapping(value = "/getLatestNote")
	public ModelAndView getLatestNote(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("device_id") String device_id,@RequestParam("CODE") String verificationCode) {
		return noteService.getLatestNote(uid, bookid,device_id,verificationCode);
	}
	
	
	@RequestMapping(value = "/isReading")
	public ModelAndView isReading(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("device_id") String device_id,@RequestParam("CODE") String verificationCode) {
		return noteService.isReading(uid, bookid,device_id,verificationCode);
	}
	
	
	@RequestMapping(value = "/isNoteExist")
	public ModelAndView isNoteExist(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("CODE") String verificationCode) {
		return noteService.isNoteExists(uid, bookid,verificationCode);

	}

	/**
	 * 获取笔记zip包功能
	 * @param uid       用户id
	 * @param bookid    图书id
	 * @param noteid    笔记id
	 * @param request   
	 * @return 成功、失败信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/getNoteZip",method = RequestMethod.POST)
	public ModelAndView getNoteZip(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("noteid") String noteid,@RequestParam("CODE") String verificationCode,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return noteService.getNoteZipFromServer(uid, bookid, noteid,verificationCode, request, response);
	}

	/**
	 * 将图书封面图片存到数据库中
	 * @param picFile 图片文件
	 * @param bookId  图书id
	 * @param request
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/addCoverPic", method = RequestMethod.POST)
	public ModelAndView updateCoverPic(@RequestParam("picFile") MultipartFile picFile,
			@RequestParam("bookid") int bookId, HttpServletRequest request) {
		return bookService.addCoverPic(picFile, bookId);
	}

	/**
	 * 书签同步功能
	 * @param uid
	 * @param json
	 * @param request
	 * @return 成功、失败信息（首选jsp试图，如果找不到找json试图）
	 */
	@RequestMapping(value = "/bookmarkSync",method = RequestMethod.POST)
	public ModelAndView bookmarkSync(@RequestParam("uid") int uid, @RequestParam("json") String json,
			@RequestParam("CODE") String verificationCode) {
		return bookmarkService.bookmarkSync(uid, json, verificationCode);
	}

	/**
	 * 阅读位置同步
	 * http://localhost:8080/ebook-server/book/readPositionSync?uid=xx&json=xx
	 * http://localhost:8080/ebook-server/book/readPositionSync.json
	 * post数据：uid=xx；json=xx
	 * @param uid
	 * @param json
	 * @param model
	 * @param request
	 * @return 首选jsp试图，如果找不到找json试图
	 */
	@RequestMapping(value = "/readPositionSync")
	public ModelAndView readPositionSync(@RequestParam("uid") int uid, @RequestParam("json") String json, Model model,
			@RequestParam("CODE") String verificationCode) {
		return bookmarkService.readPositionSync(uid, json, verificationCode);
	}

	/**
	 * 获取所有书签信息
	 * @param uid     用户名Id
	 * @param model
	 * @param request
	 * @return 视图
	*/
	@RequestMapping(value = "/getBookmark",method = RequestMethod.POST)
	public ModelAndView getBookmark(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("markeridsJson") String markeridsJson, @RequestParam("CODE") String verificationCode) {
		return bookmarkService.getBookmark(uid, bookid, markeridsJson, verificationCode);
	}

	/**
	 * 根据书签
	 * @param uid
	 * @param markerid
	 * @param model
	 * @param request
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/deleteBookmark",method = RequestMethod.POST)
	public ModelAndView deleteBookmark(@RequestParam("uid") int uid, @RequestParam("markeridsJson") String markeridsJson,
			Model model, @RequestParam("CODE") String verificationCode) {
		return bookmarkService.deleteBookmark(uid, markeridsJson, verificationCode);
	}

	/**
	 * 获取一本书的阅读位置信息
	 * @param uid
	 * @param bookid
	 * @param model
	 * @param request
	 * @return 获取的信息
	*/
	@RequestMapping(value = "/getReadPosition")
	public ModelAndView getReadPosition(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid, Model model,
			@RequestParam("CODE") String verificationCode) {
		return bookmarkService.getReadPosition(uid, bookid, verificationCode);
	}
	
	
	
}
